package cc.laop.controller.system;

import cc.laop.business.system.ResourceBusiness;
import cc.laop.business.system.RoleBusiness;
import cc.laop.entity.system.Resource;
import cc.laop.entity.system.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("admin/system/role")
public class RoleController {

    @Autowired
    private RoleBusiness roleBusiness;

    @Autowired
    private ResourceBusiness resourceBusiness;

    @RequestMapping("list.do")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Role> roleList = roleBusiness.select(new Role());
        map.put("roleList", roleList);
        return map;
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Map<String, Object> del(String ids) {
        Map<String, Object> map = new HashMap<String, Object>();
        roleBusiness.delete(ids);
        map.put("result", "success");
        return map;
    }

    @RequestMapping("addOrEdit.do")
    @ResponseBody
    public Map<String, Object> addOrEdit(Role role, String resids) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("result", roleBusiness.insertOrUpdate(role, resids));
        return map;
    }

    @RequestMapping("reslist.do")
    @ResponseBody
    public Map<String, Object> reslist() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Resource> list = resourceBusiness.select(new Resource());
        map.put("reslist", resourceBusiness.toTree(list));
        return map;
    }
}
